"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dbcompatibility-whale
Case Name   : 函数utl_file.fgetattr获取文件的信息，包含文件是否存在、文件长度和文件块大小
Create At   : 2023/12/26
Owner       : @zou_jialiang050
Description :
    1.函数utl_file.fgetattr获取文件的信息，包含文件是否存在、文件长度和文件块大小
    2.函数utl_file.fgetattr错参
    3.函数utl_file.fgetattr无参
    4.函数utl_file.fgetattr多参
    5.函数utl_file.fgetattr少参
Expect      :
    1.成功
    2.失败
    3.失败
    4.失败
    5.失败
History     :
"""
import os
import unittest
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Whale(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.common = Common()
        self.userNode = Node('PrimaryDbUser')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.dbname = 'db_whale_63'
        self.func = 'fun_whale_63'
    
    def test_utl_file(self):
        text = "---pre1:创建A兼容性数据库;expect:成功---"
        self.logger.info(text)
        pre_cmd1 = f"drop database if exists {self.dbname};" \
            f"create database {self.dbname} dbcompatibility='A';"
        pre_msg1 = self.sh_primy.execut_db_sql(pre_cmd1)
        self.logger.info(pre_msg1)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, pre_msg1,
                      text + '执行失败')
        text = "---pre2:在A数据库中创建whale插件;expect:成功---"
        self.logger.info(text)
        pre_cmd2 = f"create extension whale;"
        pre_msg2 = self.sh_primy.execut_db_sql(pre_cmd2, dbname=self.dbname)
        self.logger.info(pre_msg2)
        self.assertIn(self.constant.extension_create_success, pre_msg2,
                      text + '执行失败')
        
        text = "---step1:获取文件的信息;expect:成功---"
        self.logger.info(text)
        sql_cmd1_1 = f'''
        CREATE OR REPLACE FUNCTION {self.func}(dir text) RETURNS void AS \$\$
            DECLARE
                f utl_file.file_type;
            BEGIN
                f := utl_file.fopen(dir, 'regress_orafce.txt', 'w');
                PERFORM utl_file.put_line(f, 'A', false);
                PERFORM utl_file.put_line(f, 'B', false);
                PERFORM utl_file.put_line(f, 'C', false);
                PERFORM utl_file.fclose_all();
            END;
            \$\$ LANGUAGE plpgsql;'''
        msg1_1 = self.sh_primy.execut_db_sql(sql_cmd1_1, dbname=self.dbname)
        self.logger.info(msg1_1)
        self.assertIn(self.constant.CREATE_FUNCTION_SUCCESS_MSG, msg1_1,
                      text + '执行失败')

        sql_cmd1_2 = f"insert into utl_file.utl_file_dir(dir,dirname) " \
            f"values(utl_file.tmpdir(),'TMPDIR');"
        msg1_2 = self.sh_primy.execut_db_sql(sql_cmd1_2, dbname=self.dbname)
        self.logger.info(msg1_2)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg1_2,
                      text + '执行失败')

        sql_cmd1_3 = f" SELECT {self.func}('TMPDIR');"
        msg1_3 = self.sh_primy.execut_db_sql(sql_cmd1_3, dbname=self.dbname)
        self.logger.info(msg1_3)
        self.common.equal_sql_mdg(msg1_3, f'{self.func}', '', '(1 row)',
                                  flag='1')

        sql_cmd1_4 = f"select fexists " \
            f"from utl_file.fgetattr('TMPDIR','regress_orafce.txt');"
        msg1_4 = self.sh_primy.execut_db_sql(sql_cmd1_4, dbname=self.dbname)
        self.logger.info(msg1_4)
        self.common.equal_sql_mdg(msg1_4, 'fexists', 't', '(1 row)', flag='1')

        sql_cmd1_5 = f"select fexists " \
            f"from utl_file.fgetattr('TMPDIR','regress_orafce2.txt');"
        msg1_5 = self.sh_primy.execut_db_sql(sql_cmd1_5, dbname=self.dbname)
        self.logger.info(msg1_5)
        self.common.equal_sql_mdg(msg1_5, 'fexists', 'f', '(1 row)', flag='1')

        sql_cmd1_6 = f"select utl_file.fremove('TMPDIR','regress_orafce.txt');"
        msg1_6 = self.sh_primy.execut_db_sql(sql_cmd1_6, dbname=self.dbname)
        self.logger.info(msg1_6)
        self.common.equal_sql_mdg(msg1_6, 'fremove', '', '(1 row)', flag='1')

        text = "---step2:函数utl_file.fgetattr()错参;expect:失败---"
        self.logger.info(text)
        sql_cmd2 = f"select utl_file.fgetattr(123,123,122,111);"
        msg2 = self.sh_primy.execut_db_sql(sql_cmd2, dbname=self.dbname)
        self.logger.info(msg2)
        self.assertIn("function utl_file.fgetattr(integer, integer, integer, "
                      "integer) does not exist", msg2, text + '执行失败')
        #
        text = "---step3:函数utl_file.fgetattr()无参;expect:失败---"
        self.logger.info(text)
        sql_cmd3 = f"select utl_file.fgetattr();"
        msg3 = self.sh_primy.execut_db_sql(sql_cmd3, dbname=self.dbname)
        self.logger.info(msg3)
        self.assertIn('function utl_file.fgetattr() does not exist', msg3,
                      text + '执行失败')

        text = "---step4:函数utl_file.fgetattr()多参;expect:失败---"
        self.logger.info(text)
        sql_cmd4 = f"select utl_file.fgetattr('tes','tes','tes','tes','tes');"
        msg4 = self.sh_primy.execut_db_sql(sql_cmd4, dbname=self.dbname)
        self.logger.info(msg4)
        # self.assertIn('invalid input syntax for integer', msg4,
        #               text + '执行失败')

        text = "---step5:函数utl_file.fgetattr()少参;expect:失败---"
        self.logger.info(text)
        sql_cmd5 = f"select utl_file.fgetattr('test','test','test');"
        msg5 = self.sh_primy.execut_db_sql(sql_cmd5, dbname=self.dbname)
        self.logger.info(msg5)
        self.assertIn('function utl_file.fgetattr(unknown, unknown, unknown) '
                      'does not exist', msg5, text + '执行失败')
    
    def tearDown(self):
        text = '----清理环境----'
        clean_cmd1 = f"drop function {self.func};"
        cl_msg1 = self.sh_primy.execut_db_sql(clean_cmd1, dbname=self.dbname)
        self.logger.info(cl_msg1)
        self.assertIn(self.constant.DROP_FUNCTION_SUCCESS_MSG, cl_msg1,
                      text + '执行失败')
        
        clean_cmd2 = f"truncate utl_file.utl_file_dir;"
        cl_msg2 = self.sh_primy.execut_db_sql(clean_cmd2, dbname=self.dbname)
        self.logger.info(cl_msg2)
        self.assertIn(self.constant.TRUNCATE_SUCCESS_MSG, cl_msg2,
                      text + '执行失败')
        
        clean_cmd3 = f"drop database {self.dbname};"
        clean_msg3 = self.sh_primy.execut_db_sql(clean_cmd3)
        self.logger.info(clean_msg3)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, clean_msg3,
                      text + '执行失败')

        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
